/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.servlet;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.dao.PostDAO;
import model.dao.UserDAO;
import model.dao.service.PostDAOService;
import model.dao.service.UserDAOService;
import model.entities.Post;
import model.entities.User;
import util.Constants;

/**
 *
 * @author Admin
 */
public class AuthencationServlet extends HttpServlet {

    UserDAOService USER_SERVICE = UserDAO.getInstance();
    PostDAOService POST_SERVICE = PostDAO.getInstance();

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");
        String password = util.MD5.encryptMD5(pwd);
        if (UserDAO.getInstance().checkLogin(userName, password)) {
            User user = USER_SERVICE.getUserByUserName(userName);
            if (user.isIsActive()) {
                HttpSession session = request.getSession();
                session.setAttribute(util.Constants.CURRENT_USER, user);
                session.setMaxInactiveInterval(30 * 60);
//            Cookie loginCookie = new Cookie("userName", userName);
//            loginCookie.setMaxAge(30 * 60);
//            response.addCookie(loginCookie);
                //               request.removeAttribute(util.Constants.MSG_RESULT);
                //               session.removeAttribute(util.Constants.MSG_RESULT);
                String urlRederect = (String) session.getAttribute(Constants.URL_REDIRECT);
                if (urlRederect != null) {
                    session.removeAttribute(Constants.URL_REDIRECT);
                    response.sendRedirect(urlRederect);
                } else {
                    try {
                        List<Post> post = POST_SERVICE.getListPost();
                        request.setAttribute("newpost", post);
                        request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
                    } catch (Exception ex) {
                        Logger.getLogger(AuthencationServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else {
                request.setAttribute(util.Constants.PAGE, "result");
                request.setAttribute(util.Constants.PAGE_RESULT, "login");
                request.removeAttribute(util.Constants.MSG_RESULT);
                request.setAttribute(util.Constants.MSG_RESULT, "Please, check your mail to the conplete registration!!!");
                request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
            }
        } else {
            request.setAttribute(util.Constants.MSG_RESULT, "Username or password incorrect!");
            request.setAttribute(util.Constants.PAGE, "login");
            request.getRequestDispatcher(util.Constants.URL_INDEX).forward(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
